package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Company;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CompanyMapper;
import com.ruoyi.system.service.ICompanyService;

/**
 * 机构管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-05
 */
@Service
public class CompanyServiceImpl implements ICompanyService 
{
    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 查询机构管理
     *
     * @param id 机构管理主键
     * @return 机构管理
     */
    @Override
    public Company selectCompanyById(Long id)
    {
        return companyMapper.selectCompanyById(id);
    }

    /**
     * 查询机构管理列表
     * 
     * @param company 机构管理
     * @return 机构管理
     */
    @Override
    @DataScope(CompanyAlias = "c")
    public List<Company> selectCompanyList(Company company)
    {
        return companyMapper.selectCompanyList(company);
    }

    /**
     * 新增机构管理
     * 
     * @param company 机构管理
     * @return 结果
     */
    @Override
    public int insertCompany(Company company)
    {
        Company companyById = companyMapper.selectCompanyById(company.getParentId());
        if(companyById !=null){
            company.setAncestors(companyById.getAncestors()+","+company.getParentId());
        }
        company.setAddTime(DateUtils.getNowDate());
        return companyMapper.insertCompany(company);
    }

    /**
     * 修改机构管理
     * 
     * @param company 机构管理
     * @return 结果
     */
    @Override
    public int updateCompany(Company company)
    {
        company.setUpdateTime(DateUtils.getNowDate());
        return companyMapper.updateCompany(company);
    }

    /**
     * 批量删除机构管理
     * 
     * @param ids 需要删除的机构管理主键
     * @return 结果
     */
    @Override
    public int deleteCompanyByIds(Long[] ids)
    {
        return companyMapper.deleteCompanyByIds(ids);
    }

    /**
     * 删除机构管理信息
     * 
     * @param id 机构管理主键
     * @return 结果
     */
    @Override
    public int deleteCompanyById(Long id)
    {
        return companyMapper.deleteCompanyById(id);
    }

    @Override
    public boolean selectCompany(Company company) {
        Long id = StringUtils.isNull(company.getId()) ? -1L : company.getId();
        Company selectCompany = companyMapper.selectCompany(company.getCompanyName(), company.getParentId());
        if(StringUtils.isNotNull(selectCompany) && selectCompany.getId().longValue() != id.longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean selectById(Long id) {
        int selectById = companyMapper.selectById(id);
        return selectById>0;
    }

    @Override
    public List<TreeSelect> selectCompanyTreeList(Company company) {
        List<Company> companyList = SpringUtils.getAopProxy(this).selectCompanyList(company);
        return buildCompanyTreeSelect(companyList);
    }

    @Override
    public List<Company> buildCompanyTree(List<Company> companyList) {
        List<Company> returnList = new ArrayList<>();
        List<Long> collect = companyList.stream().map(Company::getId).collect(Collectors.toList());
        for (Company company : companyList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(company.getParentId())){
                recursionFn(companyList,company);
                returnList.add(company);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = companyList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildCompanyTreeSelect(List<Company> companyList) {
        List<Company> companyTrees = buildCompanyTree(companyList);
        return companyTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param companyList 子级
     * @param company 父级
     */
    private void recursionFn(List<Company> companyList , Company company){
        // 得到子节点列表
        List<Company> childList = getChildList(companyList, company);
        company.setChildrenCompany(childList);
        for (Company newCompany : childList)
        {
            if (hasChild(companyList, newCompany))
            {
                recursionFn(companyList, newCompany);
            }
        }
    }

    /**
     * 获得子节点
     * @param companyList
     * @param company
     * @return
     */
    private List<Company> getChildList(List<Company> companyList, Company company){
        List<Company> returnList = new ArrayList<>();
        Iterator<Company> iterator = companyList.iterator();
        while (iterator.hasNext()){
            Company n = (Company) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == company.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Company> companyList, Company company)
    {
        return getChildList(companyList, company).size() > 0;
    }
}
